﻿Public Class Validacion_Clientes
    Dim regreso As Boolean

    Public Function Mostrar() As Boolean
        regreso = False

        dsInfo = New DataSet
        dsInfo.ReadXml(ruta_programa & "\Info.xml")

        Carga_Datos()

        Dim dtClientes As DataTable = dsInfo.Tables("CLIENTES").Clone

        For Each fila As DataRow In dsInfo.Tables("CLIENTES").Select("CCLIE LIKE 'NC%'")
            dtClientes.ImportRow(fila)
        Next

        dtClientes.Columns.Add("CLAVE", GetType(Integer))
        dtClientes.Columns("CLAVE").SetOrdinal(0)


        Me.dgDatos.DataSource = dtClientes

        dgDatos.ReadOnly = False

        dgDatos.Columns("CCLIE").Visible = False
        dgDatos.Columns("CCLIE").ReadOnly = False

        For i As Integer = 1 To dgDatos.Columns.Count - 1
            dgDatos.Columns(i).ReadOnly = True
        Next

        For i As Integer = 10 To dgDatos.Columns.Count - 1
            dgDatos.Columns(i).Visible = False
        Next

        Me.dgDatos.Visible = True


        Me.ShowDialog()
        'Me.Close()
        Return regreso
    End Function

    Private Sub dgDatos_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgDatos.DataError
        If e.ColumnIndex = 0 Then
            msg.Mostrar("La clave debe de ser un valor numerico", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnClientes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClientes.Click
        regreso = False

        For Each fila As DataGridViewRow In dgDatos.Rows
            If fila.Cells(0).Value Is DBNull.Value OrElse fila.Cells(0).Value = 0 Then
                msg.Mostrar("Debes de asignarle clave a todos los clientes antes de continuar", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        Next

        For Each fila As DataGridViewRow In dgDatos.Rows
            For Each factura As DataRow In dsData.Tables("FACTURAS").Select("CCLIE='" & fila.Cells(1).Value & "'")
                factura("CCLIE") = fila.Cells(0).Value
            Next
        Next

        Guarda_Datos()

        regreso = True

        Me.Close()
    End Sub

    Private Sub Validacion_Clientes_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.F1 Then
            btnClientes_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub Validacion_Clientes_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        dgDatos.Focus()
    End Sub
End Class